#region Using Statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using XDL.Framework;
#endregion

namespace XDL.Framework.Screens
{
    /// <summary>
    /// The pause menu comes up over the top of the game,
    /// giving the player options to resume or quit.
    /// </summary>
    /// <remarks>
    /// This class uses the ExitMenuScreen configuration property to get the game screen type.
    /// </remarks>
    public class PauseMenuScreen : 
        DialogMenuScreen
    {
        #region Fields
        Type m_exitMenuScreen;

        public event EventHandler<EventArgs> OnClose;
        public event EventHandler<EventArgs> OnExitToMainMenu;

        #endregion

        #region Initialization

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="titleTextureName">Texture name to customize pause menu.</param>
        public PauseMenuScreen()
        {         
            // menu entries
            MenuItem resumeGameMenuItem = new MenuItem(Framework_Text.RESUME_GAME);
            resumeGameMenuItem.Selected += ResumeGameMenuItemSelected;
            Controls.Add(resumeGameMenuItem);

#if DEBUG
            MenuItem dumpGameMenuItem = new MenuItem("Dump");
            dumpGameMenuItem.Selected += DumpGameMenuItem;
            Controls.Add(dumpGameMenuItem);

            MenuItem debugDrawGameMenuItem = new MenuItem("Debug Draw");
            debugDrawGameMenuItem.Selected += DebugDrawGameMenuItem;
            Controls.Add(debugDrawGameMenuItem);

            MenuItem lockCamMenuItem = new MenuItem("Lock Camera");
            lockCamMenuItem.Selected += LockCamMenuItem;
            Controls.Add(lockCamMenuItem);
#endif

            MenuItem quitGameEntry = new MenuItem(Framework_Text.TERMINATE_GAME);
            quitGameEntry.Selected += QuitGameEntrySelected;
            Controls.Add(quitGameEntry);

            // Flag that there is no need for the game to transition
            // off when the pause menu is on top of it.
            IsPopup = true;
        }
        #endregion

        #region Handle Input


        /// <summary>
        /// When the user cancels the pause menu, resume the game.
        /// </summary>
        protected override void OnCancel()
        {
            if (OnClose != null)
                OnClose(this, null);
            ExitScreen();
        }

        /// <summary>
        /// Event handler for when the Resume Game menu entry is selected.
        /// </summary>
        void ResumeGameMenuItemSelected(object sender, EventArgs e)
        {
            if (OnClose != null)
                OnClose(this, null);

            ExitScreen();
        }

#if DEBUG
        /// <summary>
        /// Dump game structure to System.Diagnostic.Console.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void DumpGameMenuItem(object sender, EventArgs e)
        {
            GameServices.Dump = true;
        }

        /// <summary>
        /// Toogle debug draw on - off
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void DebugDrawGameMenuItem(object sender, EventArgs e)
        {
            // flip/flop!
            GameServices.DebugDraw = !GameServices.DebugDraw;
        }

        /// <summary>
        /// Toggle lock camera command on/off
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void LockCamMenuItem(object sender, EventArgs e)
        {
            // flip/flop!
            GameServices.LockCam = !GameServices.LockCam;
        }
#endif

        /// <summary>
        /// Event handler for when the Quit Game menu entry is selected.
        /// </summary>
        void QuitGameEntrySelected(object sender, EventArgs e)
        {
            // Quit the game, after a confirmation message box.
            string message = Framework_Text.TERMINATE_GAME_CONFIRM;

            MessageBoxScreen messageBox = new MessageBoxScreen(message);
            messageBox.Accepted += QuitMessageBoxAccepted;
            ScreenManager.AddScreen(messageBox);
        }


        /// <summary>
        /// Event handler for when the user selects ok on the "are you sure
        /// you want to quit" message box. This uses the loading screen to
        /// transition from the game back to the main menu screen.
        /// </summary>
        void QuitMessageBoxAccepted(object sender, EventArgs e)
        {

            ExitScreen();
            //
            if (OnExitToMainMenu != null)
                OnExitToMainMenu(this, null);
        }
        #endregion

        #region Draw
        /// <summary>
        /// Draws the pause menu screen. This darkens down the gameplay screen
        /// that is underneath us, and then chains to the base MenuScreen.Draw.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            ScreenManager.FadeBackBufferToBlack(TransitionAlpha * 2 / 3);

            // actual display of text
            base.Draw(gameTime);
        }
        #endregion
    }
}
